package com.pxmeta.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.user.entity.UserBlock;
import com.pxmeta.user.mapper.UserBlockMapper;
import com.pxmeta.user.service.IUserBlockService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-07-13
 */
@Service
public class UserBlockServiceImpl extends ServiceImpl<UserBlockMapper, UserBlock> implements IUserBlockService {

    @Override
    public void blockUser(Long userId, Long blockUserId) {
        UserBlock userBlock = new UserBlock();
        userBlock.setBlockUserId(blockUserId);
        userBlock.setUserId(userId);
        this.save(userBlock);
    }

    @Override
    public UserBlock isBlock(Long currentUserId, Long blockUserId) {
        LambdaQueryWrapper<UserBlock> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserBlock::getUserId, currentUserId)
                .eq(UserBlock::getBlockUserId, blockUserId);
        return this.getOne(wrapper);
    }

    @Override
    public void unBlockUser(Long userId, Long blockUserId) {
        UserBlock block = isBlock(userId, blockUserId);
        if (null == block){
            throw new ServiceException("该用户不在黑名单中！");
        }
        this.removeById(block);
    }
}
